<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Exercises extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('Exercises_model', 'exercises');
        $login_status  = $this->check_login_status();
        $this->accountID = $_SESSION['userInfo']['AccountID'];
        $this->departmentid = $_SESSION['userInfo']['departmentid'];
        if (empty($login_status)){
            redirect('/');die;
        }
	}
    public function index()
    {
        $_SESSION['sidebar'] = "exercises";
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;
        $departmentid = $this->input->get('departmentid') ? $this->input->get('departmentid') : 0;
        $doc_cat_list = $this->exercises->get_cat_list();
        $limit = 20;
        $filter['departmentid'] = $departmentid;
        $total_rows = $this->exercises->getMyExricisesListCnt($filter);
        $base_url = '/exercises/index?';
        $page_links = $this->create_page_links($base_url,$total_rows,$limit);
        $exricisesList = $this->exercises->getMyExricisesList(" * ",$per_page, $limit,$filter);
        $data = array(
            'exricisesList'=>$exricisesList,
            'page_links'=>$page_links,
            'per_page'=>$per_page,
            'doc_cat_list'=>$doc_cat_list,
            'departmentid'=>$departmentid
            );
        $this->load->view("exercises/index", $data);
    }

    public function show(){
        $_SESSION['sidebar'] = "exercises";
        $type = $this->input->get('type') ? $this->input->get('type') : 1;
        $paperID = $this->input->get('paperID') ? $this->input->get('paperID') : 0;
        $questionInfo = $this->exercises->getQuestionInfo($type,$paperID);
        /*
         * 遍历题目进行模板拼接
         */
        $BasePath = APPPATH."smarty/templates/question/";
        $questionBody = $this->fetchSurveyTemp($questionInfo['questionInfo'],$BasePath);
        $questionTitle = $questionInfo['paperInfo'];
        $returnData = array('body'=>$questionBody,'info'=>$questionTitle);
        $this->load->view("exercises/show", $returnData);
    }
    public function answer(){
        $paperID = $this->input->post('id') ? $this->input->post('id') : 1;
        $answer = $this->input->post('data') ? $this->input->post('data') : 1;
        $questionAnser = $this->exercises->getPaperAnswerInfo($paperID);
        //计算得分
        $score = $this->exercises->getScoreByAnswer($answer,$questionAnser);
        echo $this->response_data(array('score'=>$score));
    }
    public function success(){
        $_SESSION['sidebar'] = "exercises";
        $score = $this->input->get('score') ? $this->input->get('score') : 0;
        if($score<60){
            $notice = "未达标！";
        }else{
            $notice = "已达标！";
        }
        $data = array('score'=>$score,'notice'=>$notice);
        $this->load->view("exercises/success", $data);
    }
    public function mywrong(){
        $_SESSION['sidebar'] = "myexercises";
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;
        $limit = 11;
        $questionList = $this->exercises->getWrongQuestion($this->accountID,$this->departmentid,$per_page,$limit);
        $questionList = $this->exercises->arrayMosaic($questionList);
        $BasePath = APPPATH."smarty/templates/qwrong/";
        $questionBody = $this->fetchSurveyTemp($questionList,$BasePath);
        $total_rows = $this->exercises->getWrongQuestionCnt($this->accountID,$this->departmentid);
        $base_url = '/exercises/mywrong?';
        $page_links = $this->create_page_links($base_url,$total_rows,$limit);
        $data=array(
            'questionList'=>$questionBody,
            'page_links'=>$page_links,
            'per_page'=>$per_page
        );
        $this->load->view("exercises/mywrong", $data);
    }
  

}
/* End of file Index.php */
/* Location: ./application/controllers/video.php */
